
<template>
  <div>
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
      <el-row>
        <el-col :span="7">
          <el-form-item label="客商名称" prop="supName" label-width="90px">
            <el-input v-model="form.supName" readonly :disabled="disableForm || optType !== 'add'" @click.native="optType === 'add' ? showSupplierListModal = true : null">
              <i slot="suffix" class="el-input__icon el-icon-circle-plus-outline" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="统一社会信用代码" prop="supUnifiedSocialCreditCode" label-width="150px">
            <el-input v-model="form.supUnifiedSocialCreditCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="身份证" prop="supIdentifyNumber" label-width="90px">
            <el-input v-model="form.supIdentifyNumber" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="7">
          <el-form-item label="客商编号" prop="supNumber" label-width="90px">
            <el-input v-model="form.supNumber" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="考核编号" prop="basNumber" label-width="150px">
            <el-input v-model="form.basNumber" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="评价分值" prop="basFraction" label-width="90px">
            <el-input v-model="form.basFraction" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="7">
          <el-form-item label="评价等级" prop="basRating" label-width="90px">
            <el-select v-model="form.basRating" :disabled="disableForm" style="width: 100%;" placeholder="请选择">
              <el-option v-for="item in ratingLevels" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="评价日期区间" prop="basRangeTime" label-width="150px">
            <el-input v-model="form.basRangeTime" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="备注" prop="basRemark" label-width="90px">
            <el-input v-model="form.basRemark" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="综合评价" prop="basDescription" label-width="90px">
        <el-input v-model="form.basDescription" :disabled="disableForm" />
      </el-form-item>
      <!-- <el-form-item label="">
        <div style="display: flex;align-items: center;">
          <el-button style="margin-right:10px;margin-top:2px;" size="small" type="primary" @click="showTreeModal = true">{{ typeLabel }}</el-button>
          <uploader v-model="fileList" :disabled="disableForm" :can-download="optType==='detail'" />
        </div>
      </el-form-item> -->
    </el-form>
    <div style="padding: 0 10px 0 26px;">

      <div style="padding-bottom: 10px;">
        <el-button :disabled="disableForm" type="primary" @click="showTargetSetListModal = true">引入考核指标</el-button>
        <el-button :disabled="disableForm" type="primary" @click="saveSupplier">删除考核指标</el-button>
      </div>
      <com-table
        v-model="selectRow"
        highlight-current-row
        :data="targetList"
        :columns="columns"
        :no-page="true"
      >
        <template v-slot:basFraction="{row}">
          <el-input v-model="row.basFraction" />
        </template>
        <template v-slot:basRemark="{row}">
          <el-input v-model="row.basRemark" :disabled="disableForm" />
        </template>
      <!-- <template v-slot:options="{row}">
        <el-button type="primary" size="small" @click="editBuildUnit(row)">编辑</el-button>
        <el-button type="danger" size="small" @click="delBuildUnit(row)">删除</el-button>
      </template> -->
      </com-table>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </div>
  </div>

  <!-- <supplier-select-list :type="type" :show-modal.sync="showSupplierListModal" @getSelectedData="selectSupplier" />
    <target-set-select-list :type="type" :show-modal.sync="showTargetSetListModal" @getSelectedRows="getSelectedRows" /> -->
</template>

<script>
import { addSupplierAssessInfo, updateSupplierAssessInfo, getSupplierAssessInfoDetail } from '@/api/travellingTraderManage'
import { getAreaList } from '@/api/common'
import { identifyTypes, organizationLevels, ratingLevels } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
// import supplierSelectList from './supplierSelectList.vue'
// import targetSetSelectList from './targetSetSelectList.vue'
// import supplierTree from './supplierTree.vue'
export default {
  name: 'AddJudge',
  components: {
    fileUploader
    // targetSetSelectList,
    // supplierSelectList
  },
  props: {
    type: {
      type: String,
      default: ''
    },
    optType: {
      type: String,
      default: 'add'
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      selectRow: null,
      targetList: [],
      columns: [
        {
          label: '编号',
          prop: 'number'
        },
        {
          label: '名称',
          prop: 'name'
        },
        {
          label: '评定指标',
          prop: 'description'
        },
        {
          label: '指标分值',
          prop: 'fraction'
        },
        {
          label: '评定分值',
          // prop: 'fraction',
          slot: 'basFraction'
        },
        {
          label: '备注',
          // prop: 'fraction',
          slot: 'basRemark'
        }
      ],
      showSupplierListModal: false,
      showTargetSetListModal: false,
      options: [],
      ratingLevels,
      identifyTypes,
      organizationLevels,
      addType: '', // same   under
      visible: false,
      supplierInfo: {},
      form: {
        supName: '',
        supNumber: '',
        supUnifiedSocialCreditCode: '',
        supIdentifyNumber: '',
        basFraction: '',
        basNumber: '',
        basDescription: '',
        basRangeTime: '',
        basRemark: '',
        basRating: ''
      },
      fileList: [
      ],
      rules: {
        // name: [
        //   { required: true, message: '单位名称', trigger: 'change' }
        // ],
        // unifiedSocialCreditCode: [
        //   { required: true, message: '统一社会信用代码', trigger: 'change' }
        // ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    typeLabel() {
      return this.type === '1' ? '分包专业' : '供应商类别'
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.businessId) {
          this.getDocDetail(newData.businessId)
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    getSelectedRows(data) {
      this.targetList = this.targetList.concat(data.map(item => {
        return {
          ...item,
          basFraction: '',
          basRemark: ''
        }
      }))
    },
    selectSupplier(data) {
      if (data) {
        this.supplierInfo = data
        const { name, number, unifiedSocialCreditCode, identifyNumber } = data
        this.form.supName = name
        this.form.supNumber = number
        this.form.supUnifiedSocialCreditCode = unifiedSocialCreditCode
        this.form.supIdentifyNumber = identifyNumber
      }
    },
    getDocDetail(id) {
      getSupplierAssessInfoDetail({ supplierAssessInfoId: id }).then(res => {
        const {
          lstAttachment,
          lstSupplierAssessIndex,
          supplierAssessInfo
        } = res.data || {}
        if (supplierAssessInfo) {
          Object.keys(this.form).forEach(key => {
            this.form[key] = supplierAssessInfo[key]
          })
          this.supplierInfo.id = supplierAssessInfo.supplierInfoId
        }
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        if (lstSupplierAssessIndex && lstSupplierAssessIndex.length > 0) {
          this.targetList = lstSupplierAssessIndex.map(item => {
            const {
              basFraction,
              basRemark,
              idxDescription: description,
              idxFraction: fraction,
              idxName: name,
              idxNumber: number,
              id,
              supplierAssessInfoId,
              supplierIndexInfoId
            } = item
            return {
              name,
              number,
              basFraction,
              basRemark,
              fraction,
              description,
              id,
              supplierAssessInfoId,
              supplierIndexInfoId
            }
          })
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    saveSupplier() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      if (!this.supplierInfo.id) {
        this.$message({
          type: 'error',
          message: `请选择${this.typeTitle}`
        })
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          // const formData = JSON.parse(JSON.stringify(
          //   this.form
          // ))
          // const {
          //   fraction,
          //   number,
          //   description,
          //   rangeTime,
          //   remark,
          //   rating
          // } = formData
          const params = {
            supplierAssessInfo: {
              ...this.form,
              supplierInfoId: this.supplierInfo.id,
              basType: this.type,
              ...this.optType === 'edit' ? {
                id: this.data.saiId
              } : {}
            },
            ...this.targetList.length ? {
              lstSupplierAssessIndex: this.targetList.map(item => {
                return {
                  basFraction: item.basFraction,
                  basRemark: item.basRemark,
                  basType: this.type,
                  ...this.optType === 'edit' ? {
                    id: item.id,
                    supplierAssessInfoId: this.data.saiId,
                    supplierIndexInfoId: item.supplierIndexInfoId
                  } : { supplierIndexInfoId: item.id }
                }
              })
            } : {},
            lstAttachment: this.fileList
          }

          const fn = this.optType === 'add' ? addSupplierAssessInfo : updateSupplierAssessInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              // this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>

